*****************************************************************
*																*
*				Title: regressions_metal_app		 	 	   	*		
*															    *
*****************************************************************

/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we run regressions for the metal sector and output the regressions' results for the appendix
	
 
		
+ Inputs:

		* 1. panel_metallurgy.dta


==============================  TOP MATTER ==============================*/



	*set paths
	global proc_data "`pdir'/data"
	global output "`pdir'/outputs/appendix"


******************************** END HEADER ********************************

use "$proc_data/panel_metallurgy.dta", clear

	
***************************************************************************
***********************			TABLES APPENDIX 		*******************
***************************************************************************	



******* 	Table A.3 (Panel A, B, C): Summary Statistics	*******
*Some rounding is applied by hand after generating the table, as the frmttable command is not flexible in adjusting digits after the comma

********************************************************************

* make empty matrix
mat table_a3_a = J(6,10,.)

** Panel A, LHS, Around 1800:

sum logYmeanoverL_real L_total  exit_1811  if year==1811

local counter = 1

foreach x of varlist logYmeanoverL_real L_total  exit_1811 {	
	sum `x' if year==1811
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a3_a[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel A RHS: starts in column 6
local counter = 1

foreach x of varlist logYmeanoverL_real L_total dummy_water_mill dummy_steam_mill dummy_other_mill {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a3_a[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	if `counter' == 2 {
		local counter = 4
	}
	else {
		local counter = `counter' + 1
	}
} 

matlist table_a3_a


frmttable using "$output/Tables/Table_A3A.tex", statmat(table_a3_a) replace ///
	landscape tex fragment ///
	rtitles("Log (output per worker)" \ "Number of workers" ///
	\ "Exit dummy" \ "Water power" \ "Steam power" \ "Other power") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
			
  preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A3A.tex"
	local tex2 "$output/Tables/Table_A3A.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Log","Log")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 


********************************************************************
* make empty matrix
mat table_a3_b = J(6,10,.)

** Panel B, LHS, Around 1800:

sum logYmeanoverL_real L_total  exit_1811  if year==1811

local counter = 1

foreach x of varlist highsflow closecoal share_forest lnoutputdensity_commune lnconscripts_pc near_battles10 {	
	sum `x' if year==1811
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a3_b[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel B RHS: starts in column 6
local counter = 1

foreach x of varlist highsflow closecoal share_forest lnoutputdensity_commune lnconscripts_pc near_battles10 {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a3_b[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
}

matlist table_a3_b

frmttable using "$output/Tables/Table_A3B.tex", statmat(table_a3_b) replace ///
	landscape tex fragment ///
	rtitles("Access to high streamflow" \ "Proximity to coal" ///
	\ "Share of forest area" \ "Production density" \ "log(conscripts pc)" \ "Near Battles") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
	
 
  preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A3B.tex"
	local tex2 "$output/Tables/Table_A3B.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Access to high","Access to high")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 


********************************************************************

* make empty matrix
mat table_a3_c = J(2,10,.)

** Panel C, LHS, Around 1800:
local counter = 1

foreach x of varlist distance_above_p90_1800 distance_london {	
	sum `x' if year==1811
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a3_c[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel C RHS: starts in column 6
local counter = 1

foreach x of varlist distance_above_p90_1800 distance_london {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a3_c[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
}

matlist table_a3_c

frmttable using "$output/Tables/Table_A3C.tex", statmat(table_a3_c) replace ///
	landscape tex fragment ///
	rtitles("Dist to p90" \ "Dist to London") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
	
preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A3C.tex"
	local tex2 "$output/Tables/Table_A3C.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Dist to p90","Dist to p90")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 
	
	
	
************	Table A.6 (Panel A). ********************************************
*** Annual Productivity Growth (in %) at Different Quantiles of the Distribution	 	
*** Same as Table 1B. 


estimates clear
quietly{
	eststo: xi:reg logYmeanoverL_real_pa i.year,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(90) vce(robust)
}		

estout using "$output/Tables/Table_A6A.tex", replace label drop(_cons) order($show ) ///
    rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
    mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
    cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none)
*redundancy in reported number of observations in the last column is fixed by hand after generating the table"
	

************	Table A.6 (Panel B):  ******************************************
***Productivity Growth by Quantiles – Non-imputed internal labor  

	
preserve 
drop if imputation==1
estimates clear
quietly{
	eststo: xi:reg logYmeanoverL_real_pa i.year ,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A6B.tex", replace label drop (_cons) order($show ) ///
rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 	 
restore		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table"


************	Table A.6 (Panel C): *******************************************
*** Productivity Growth by Quantiles – Plant-specific prices   ************	


preserve
drop if logYmeanoverL_real_pa==logYoverL_real_pa & year==1811
estimates clear
quietly{
	eststo: xi:reg logYoverL_real_pa i.year ,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A6C.tex", replace label drop (_cons) order($show ) ///
rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 	 
restore		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table"




************	Table A.18: Productivity of Exiting Relative to Surviving Plants (column 2)	 	***********
*** Needs to be manually put together for one table in the paper

estimates clear
quietly {
reg logYmeanoverL_real exit_1811 if year == 1811 , r
}
estout using "$output/Tables/Table_A18_metal.tex", replace label drop (_cons) ///
rename (exit_1811 "Exit dummy") ///
 prefoot("\hline") stats(N, layout(@ "\multicolumn{1}{c}{@}") fmt( %9.0g) labels("N")) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.1cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.2cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(Metallurgy) 	 
	

	
	
************	Table A.20: ************************************************
*** Metallurgy in 1811: Productivity and Plants' Age Profile    ************
estimates clear
quietly{
	eststo: reg logYmeanoverL_real entrant_1811  if year == 1811, r
	eststo: reg logYmeanoverL_real entrant_1811 logL_total  if year == 1811, r
		}

estout using "$output/Tables/Table_A20.tex", replace label drop (_cons) order($show ) ///
 prefoot("\hline") stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(2 %9.0g) labels("R$^2$" "N")) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.1cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.2cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 	

		
		
		
************	Table A.21: ************************************************
*** Metallurgy in 1840: Productivity and Plants' Age Profile    ************
	
preserve
label var dummy_steam_mill "Steam power"
label var dummy_water_mill "Water power"
label var dummy_other_mill "Other power"

estimates clear
quietly{
	eststo: reg logYmeanoverL_real entrant_1840  if year == 1840, r
	eststo: reg logYmeanoverL_real entrant_1840 dummy_water_mill  if year == 1840, r
	eststo: reg logYmeanoverL_real entrant_1840 dummy_steam_mill  if year == 1840, r
	eststo: reg logYmeanoverL_real entrant_1840 dummy_other_mill  if year == 1840, r
	eststo: reg logYmeanoverL_real entrant_1840 logL_total  if year == 1840, r
	}
estout using "$output/Tables/Table_A21.tex", replace label drop (_cons) order($show ) ///
 prefoot("\hline") stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(3 %9.0g) labels("R$^2$" "N")) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.1cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.2cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 

restore


		
************	Table A.22 (Panel B): ******************************************
*** Productivity Growth by Quantiles – Controlling for Region FE    

estimates clear
quietly{
	eststo: xi:reg logYmeanoverL_real_pa i.year i.id_region,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYmeanoverL_real_pa i.year i.id_region, quantile(10) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year i.id_region, quantile(25) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year i.id_region, quantile(50) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year i.id_region, quantile(75) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year i.id_region, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A22B.tex", replace label drop( _Iid_region_*) order($show ) ///
rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 	 ///
	indicate("Region FE=_cons", labels("\multicolumn{1}{c}{\checkmark}" "\multicolumn{1}{c}{ }"))			
*redundancy in reported number of observations in the last column is fixed by hand after generating the table	

	
	

************	Table A.25 Cols 2 and 5:    ***********************************	
** Correlation between logY/L and conscripts/capita 
*** (Needs to be manually put together for one table in the paper)

estimates clear 
	eststo: reg logYmeanoverL_real lnconscripts_pc if year==1811, cl(id_region)
	eststo: reg logYmeanoverL_real lnconscripts_pc if year==1840, cl(id_region)
	
estout using "$output/Tables/Table_A25_metal.tex", replace label drop(_cons) order($show ) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	stats(N, layout(@ "\multicolumn{1}{c}{@}") fmt(%9.0g) labels("N")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 
	
	
	
	
************	Table A.26 Col 2     ******************************************		
** Correlation between logY/L and battles within 10k in 1840

estimates clear
eststo: reg logYmeanoverL_real near_battles10 if year==1840, vce(robust)

estout using "$output/Tables/Table_A26_metal.tex", replace label drop(_cons) order($show ) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	stats(N, layout(@ "\multicolumn{1}{c}{@}") fmt(%9.0g) labels("N")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 


	
	
************	Table A.27 (Panel B): ****************************************
*** Productivity Growth by Quantiles – Plants with at Least 10 Workers    	

estimates clear
quietly{
	eststo: xi:reg logYmeanoverL_real_pa i.year if L_total>=10,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYmeanoverL_real_pa i.year if L_total>=10, quantile(10) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year if L_total>=10, quantile(25) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year if L_total>=10, quantile(50) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year if L_total>=10, quantile(75) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa i.year if L_total>=10, quantile(90) vce(robust)
}

estout using "$output/Tables/Table_A27B.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table	
	
	
	
	
************	Table A.29 (Panel B): Productivity by Quantiles – Controlling for Number of Workers    ************	

estimates clear
quietly{
	eststo: xi:reg logYmeanoverL_real_pa  i.year logL_total,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYmeanoverL_real_pa  i.year logL_total, quantile(10) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa  i.year logL_total, quantile(25) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa  i.year logL_total, quantile(50) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa  i.year logL_total, quantile(75) vce(robust)
	eststo: xi:qreg logYmeanoverL_real_pa  i.year logL_total, quantile(90) vce(robust)
}

estout using "$output/Tables/Table_A29B.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Metallurgy\ (1811-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 	
*redundancy in reported number of observations in the last column is fixed by hand after generating the table	
	
	

	
	
	
